<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi as UserApi;
use Think\Model;

/**
 * 报损管理控制器
 */
class LossController extends AdminController {
    
    /**
     * 申请报损管理首页面
     * @author James 2016-01-26
     */
    public function index() {
        $pname = I('pname');

        if ($pname) {
            $map['pname']=array('like','%'.$pname.'%');
        }
        $list = $this->lists('loss',$map,'createdAt desc,id desc');
        int_to_string_loss($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }
    
    /**
     * 审核报损管理首页面
     * @author James 2016-01-26
     */
    public function verify() {
        $pname = I('pname');

        if ($pname) {
            $map['pname']=array('like','%'.$pname.'%');
        }
        $map['status'] = 0;
        $map['confirmerId'] = UID;
        $list = $this->lists('loss',$map,'createdAt desc,id desc');
        int_to_string_loss($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }
    
    /**
     * 新增报损
     * @author James 2016-01-26
     */
    public function add(){
        if(IS_POST){
            $buyer = I('buyer');
            $pname_array = I('pname');
            $cnt_array = I('cnt');
            $reason_array = I('reason');
            for ($i = 0; $i < count($pname_array); $i++) {
                if ($pname_array[$i] == "" || $cnt_array[$i] == ""||$buyer=="") {
                    $this->error("报损信息不能为空");
                }
            }
            for ($i = 0; $i < count($pname_array); $i++) {
                $dataList[] = array(
                    'pname' => $pname_array[$i],
                    'num' => $cnt_array[$i],
                    'confirmer' => explode(',',$buyer)[1],
                    'confirmerId' => explode(',',$buyer)[0],
                    'reason' => $reason_array[$i],
                    'createdAt' => NOW_TIME,
                    'updatedAt' => NOW_TIME,
                    'createdUser' => UID,
                    'updatedUser' => UID
                );
            }
            if(D('loss')->addAll($dataList)){
                $this->success('操作成功！', U('index'));
            }else{
                $this->success("操作失败");
            }
        }else{
            $Model = new Model();
            $sql = "select pname from gr_inventory a where a.status = 1 group by pname";
            $prolist = $Model -> query($sql);
            $this->assign( 'product', json_encode($prolist) );
            
            $sql = "select a.uid,b.nickname 
                from gr_auth_group_access a,gr_member b
                where a.group_id = 4
                and a.uid = b.uid
                and b.status = 1
                order by b.nickname";
            $cwlist = $Model -> query($sql);
            $this->assign( 'cwlist', $cwlist);
                    
            $this->display();
        }
    }
    
    /**
     * 审核商品
     * @author James
     * 2016-02-03
     */
    public function pass($id = 0) {
        if ($id) {
            $classify = M("loss");
            
            $data = array('status'=>1,'updatedAt'=>NOW_TIME);
            $result = $classify-> where(array('id'=>$id))->setField($data);
            
            if($result){
                $this->success('审核成功！', U('verify'));
            }else{
                $this->error("审核失败！");
            }
        }
    }
}
